﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using PluginSurrogate;
using InsertFromSkyDrive;
using System.Reflection;
using System.Diagnostics;

namespace InsertFromSkyDriveProc
{
    static class Program
    {
        /// <param name="args">
        /// args[0] should be an action to perform--CreateContent or Options.
        /// args[1] should be Writer's program directory.
        /// </param>
        [STAThread]
        static void Main(string[] args)
        {
            // We have to start a new AppDomain in order to add Writer's directory
            // to the PrivateBinPath
            string writerDir = args[1];
            AppDomainSetup ads = new AppDomainSetup()
            {
                PrivateBinPath = writerDir,
                AppDomainInitializerArguments = args,
                AppDomainInitializer = MainInternal
            };
            AppDomain.CreateDomain("WorkerDomain", AppDomain.CurrentDomain.Evidence, ads);
        }

        private static void MainInternal(string[] args)
        {
            Application.EnableVisualStyles();

            switch (args[0])
            {
                case "CreateContent":
                    SurrogateHelper.PerformOperation((IWin32Window owner, object data) =>
                        {
                            CreateContentData ccd = (CreateContentData)data;
                            PluginSettings settings = ccd.Settings;
                            using (frmMain main = new frmMain(settings))
                            {
                                DialogResult result = main.ShowDialog(owner);
                                return new CreateContentData
                                {
                                    Settings = settings,
                                    Content = result == DialogResult.OK ? main.FinalEmbedCode : null
                                };
                            }
                        });
                    break;
                case "Options":
                    SurrogateHelper.PerformOperation((IWin32Window owner, object data) =>
                        {
                            PluginSettings settings = (PluginSettings) data;
                            frmOptions opt = new frmOptions(settings);
                            opt.ShowDialog(owner);
                            return settings;
                        });
                    break;
            }
        }
    }
}
